﻿using System;
using System.Collections.Generic;
using DATA.Base;
using DATA.BusinessObjects;
using DATA.ManagerObjects;
using NHibernate;

namespace BUS
{
    public class MatDoSuDungPhongBUS
    {
        #region Bình - Lấy mật độ sử dụng phòng theo tháng                
        public static Matdosudungphong LayMatDoSuDungPhongTheoThang(int thang)
        {
            Matdosudungphong mdsdp = null;
            IManagerFactory MF = new ManagerFactory();
            IMatdosudungphongManager MDSDPM = MF.GetMatdosudungphongManager();
            String hql = "FROM Matdosudungphong mdsdp "
                        + "WHERE mdsdp.Thang = " + thang;

            IList<Matdosudungphong> list = MDSDPM.GetByQuery(hql);
            if (list.Count > 0)
                mdsdp = list[0];
            return mdsdp;
        }
        #endregion

        #region Bình - Lấy mật độ sử dụng phòng cuối cùng
        public static Matdosudungphong LayMatDoSuDungPhongCuoiCung()
        {
            Matdosudungphong mdsdp = null;
            IManagerFactory MF = new ManagerFactory();
            IMatdosudungphongManager MDSDPM = MF.GetMatdosudungphongManager();
            String hql = "FROM Matdosudungphong mdsdp "
                        + "WHERE length(mdsdp.Id) = (Select MAX(length(mdsdp.Id)) "
                                                + "from Matdosudungphong mdsdp) "
                        + "ORDER BY mdsdp.Id desc";

            IList<Matdosudungphong> list = MDSDPM.GetByQuery(hql);
            if (list.Count > 0)
                mdsdp = list[0];
            return mdsdp;
        }
        #endregion

        #region Bình - Thêm mật độ sử dụng phòng
        public static Boolean ThemMatDoSuDungPhong(Matdosudungphong mdsdp)
        {
            Boolean flag = false;
            ISession isession = NHibernateSessionManager.Instance.CreateISession();
            ITransaction iTrans = null;
            try
            {
                iTrans = isession.BeginTransaction();
                isession.Save(mdsdp);
                iTrans.Commit();
                flag = true;
            }
            catch (Exception ex)
            {
                iTrans.Rollback();
                isession.Close();
                throw new Exception("Lỗi", ex);
            }
            return flag;
        }
        #endregion
    }
}
